// 作业：建一个用户表（有姓名,年龄,性别,创建时间,修改时间）,使用模块加mysql模块,实现对数据的增删改查
    //  修改时记得，把修改时间改成修改时的时间，能实现分页（选做）
let mysql = require("mysql");
let connction = mysql.createConnection({host: "127.0.0.1",user: "root", password: "root", port: "3306", database: "db"});
connction.connect();

//增
function add(name,age,sex) {
    let startime = new Date().getTime();
    let sql = "insert into users(name,age,sex,startime) value ('"+name+"','"+age+"','"+sex+"','"+startime+"') ";
    return sql;
}

//删
function shan(id) {
    let sql = "delete from users where id =" +id +"";
    return sql;
}

//改
function change(xin,old,id) {
    let time1 = Date.parse(new Date());
    let change = "update users set " + xin + "=" + "'" + old + "'" + ",updatetime= " + "'" + time1 + "'" + "where id=" + id + "";
    return change;
}

//查
function search() {
    let sql = "select * from users";
    return sql;
}

//执行sql语句
//执行增加的语句
connction.query(add("李子玉","18","女"),(err,data) => {
    console.log(err);
    console.log(data);
});

//执行删除的语句
connction.query(shan("1"),(err,data) => {
    console.log(err);
    console.log(data);
});

//执行修改的语句
connction.query(change("name","李子明","2"),(err,data) => {
    console.log(err);
    console.log(data);
});

//执行查询的语句
connction.query(search(),(err,data) => {
    console.log(err);
    console.log(data);
});

connction.end();
